public class MyLinkedList implements IList{
    static class Listnode{
        public int val;
        public Listnode next;
        public Listnode prev;
        public Listnode(int val){
            this.val=val;
        }

    }
    public Listnode head;
    public Listnode last;

    @Override
    public void addFirst(int data) {
        Listnode node=new Listnode(data);
        if(head==null){
            last=head=node;
        }
        head.prev=node;
        node.next=head;
        head=node;
    }

    @Override
    public void addLast(int data) {
        Listnode node=new Listnode(data);
        if(last==null){
            last=head=node;
        }
        last.next=node;
        node.prev=last;
        last=node;
    }

    @Override
    public void addIndx(int index, int data) {
        int len=size();
        if(index<0&&index>len){
            return ;
        }
        if(index==0){
            addFirst(data);
        }
        if(index==len){
            addLast(data);
        }
        Listnode node=new Listnode(data);
        Listnode newh=head;
        while(index-1!=0){
            newh=newh.next;
            index--;
        }
        newh.next.prev=node;
        node.next=newh.next;
        newh.next=node;
    }

    @Override
    public boolean contains(int key) {
        Listnode node=head;
        while(node!=null){
            if(node.val==key){
                return true;
            }
            node=node.next;
        }
        return false;
    }

    @Override
    public int size() {
        int count=0;
        Listnode node=head;
        while(node!=null){
            count++;
            node=node.next;
        }
        return count;
    }

    @Override
    public void clear() {
        Listnode cur=head;
        while(cur!=null){
            Listnode curN=cur.next;
            cur.next=null;
            cur.prev=null;
            cur=curN;
        }
        head=last=null;
    }

    @Override
    public void display() {
        Listnode node=head;
        while(node!=null){
            System.out.print(node.val+" ");
            node=node.next;
        }
    }

    @Override
    public void remove(int key) {
        if(head==null){
            return ;
        }
        Listnode node=head;
        while(node!=null){
            if(node.val==key){
                if(node==last){
                    node.prev.next=null;
                    last=node.prev;
                    if(last!=null){
                        last=node.prev;
                    }

                }
                else if(node==head){
                    node.next.prev=null;
                    head=node.next;
                    if(head!=null){
                        head=node.next;
                    }
                }else{
                    node.prev.next=node.next;
                    node.next.prev=node.prev;
                }
            }
            node=node.next;
        }
    }

    @Override
    public void removeAllkey(int key) {
        Listnode node=head;
        Listnode node1=head.next;
        while(node1!=null){
            if(node1.val==key){
                node.next=node1.next;
                node1.prev=node;
                node1=node1.next;
            }else{
                node=node1;
                node1=node1.next;

            }
            if(head.val==key){
                head=head.next;
                head.prev=null;
            }
        }
    }
}